﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Freedom.Models.ViewModels;
using Freedom.Models.Interfaces;
using Freedom.Models.Services;
using Freedom.Models;

namespace Freedom.Controllers
{
    /// <summary>
    /// 联系方式控制器
    /// autor: Penn Yao
    /// </summary>
    [HandleError]
    public class ContactController : Controller
    {
        ContactViewModel viewModel;
        IMessageInfoService messageInfoService;

        public ContactController()
        {
            if (viewModel == null)
                viewModel = new ContactViewModel();
            if (messageInfoService == null)
                messageInfoService = new MessageInfoService();
        }

        public ActionResult Index()
        {
            return View(viewModel);
        }

        [HttpPost]
        public ActionResult Post([Bind(Include = "Name, Email, Title, Content")]tb_MessageInfo messageInfo)
        {
            if (messageInfoService != null)
            {
                messageInfo.Name = messageInfo.Name.Equals("姓名") ? null : messageInfo.Name;
                messageInfo.Email = messageInfo.Email.Equals("邮箱") ? null : messageInfo.Email;
                messageInfo.Title = messageInfo.Title.Equals("主题") ? null : messageInfo.Title;
                messageInfo.Content = messageInfo.Content.Equals("内容") ? null : messageInfo.Content;
                messageInfo.CreateTime = DateTime.Now;
                messageInfo.UpdateTime = DateTime.Now;
                messageInfoService.addInfo(messageInfo);
            }

            if (viewModel != null)
                viewModel.messageInfo = messageInfo;

            return RedirectToAction("Index", "Contact");
        }
    }
}
